package com.jinsilu.freight.ui.activity;

import android.content.Intent;
import android.mtp.MtpConstants;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.SearchView;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.fondesa.recyclerviewdivider.DividerDecoration;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.jinsilu.freight.R;
import com.jinsilu.freight.common.MyActivity;
import com.jinsilu.freight.helper.CommonHelper;
import com.jinsilu.freight.http.model.HttpData;
import com.jinsilu.freight.http.request.GetBankNameListApi;
import com.jinsilu.freight.http.response.DictBean;
import com.jinsilu.freight.other.MTLConstants;
import com.jinsilu.freight.ui.adapter.BankNameListAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 保险公司列表
 *
 * @author yangyang
 * @time 2020-11-10 15:49
 */
public class BankCompanyListActivity extends MyActivity {

    private SearchView mSearchView;
    private SmartRefreshLayout refresh;
    private RecyclerView recycler;
    private BankNameListAdapter mAdapter;

    private int pageNumber = 1;
    private String bankName = "";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bank_name_list;
    }

    @Override
    protected void initView() {

        mSearchView = findViewById(R.id.search_view);

        refresh = findViewById(R.id.refresh);
        refresh.setEnableLoadMore(false);

        recycler = findViewById(R.id.recycler);

        recycler.setLayoutManager(new LinearLayoutManager(getContext()));
        DividerDecoration.builder(getContext())
                .asSpace().size(10, TypedValue.COMPLEX_UNIT_DIP)
                .showSideDividers()
                .showFirstDivider()
                .showLastDivider()
                .build()
                .addTo(recycler);
        mAdapter = new BankNameListAdapter(new ArrayList<>());
        mAdapter.setEmptyView(R.layout.layout_empty_data_view);
        recycler.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                DictBean dictBean = (DictBean) adapter.getItem(position);
                Intent intent = getIntent();
                intent.putExtra("bank", dictBean);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        //默认就是搜索框展开
        mSearchView.setIconified(false);
        //一直都是搜索框，搜索图标在输入框左侧（默认是内嵌的）
        mSearchView.setIconifiedByDefault(false);
        mSearchView.setSubmitButtonEnabled(true);

        // 设置文字颜色
        EditText editText = mSearchView.findViewById(androidx.appcompat.R.id.search_src_text);
        editText.setHintTextColor(ContextCompat.getColor(this, R.color.black));
        editText.setTextColor(ContextCompat.getColor(this, R.color.black));

        mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            //文字输入完成，提交的回调
            @Override
            public boolean onQueryTextSubmit(String s) {
                pageNumber = 1;
                bankName = s;
                getBankName();
                return false;
            }

            //输入文字发生改变
            @Override
            public boolean onQueryTextChange(String s) {
                LogUtils.e("onQueryTextChange: " + s);
                if (TextUtils.isEmpty(s)) {
                    pageNumber = 1;
                    bankName = "";
                    getBankName();
                }
                return false;
            }
        });
        refresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageNumber += 1;
                getBankName();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNumber = 1;
                getBankName();
            }
        });
    }

    @Override
    protected void initData() {
        getBankName();
    }

    private void getBankName() {
        // 获取保险信息列表
        EasyHttp.get(this)
                .api(new GetBankNameListApi()
                        .setBankName(bankName).setPageNum(String.valueOf(pageNumber)).setPageSize(MTLConstants.PAGE_SIZE))
                .request(new HttpCallback<HttpData<DictBean>>(this) {
                    @Override
                    public void onSucceed(HttpData<DictBean> data) {
                        CommonHelper.finishedLoading(refresh);
                        float total = data.getTotal();
                        refresh.setEnableLoadMore(total / Float.parseFloat(MTLConstants.PAGE_SIZE) > pageNumber);
                        List<DictBean> dictBeans = data.getRows();
                        if (pageNumber == 1)
                            mAdapter.setList(dictBeans);
                        else
                            mAdapter.addData(dictBeans);
                    }
                });
    }
}
